package gui;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.Font;
import java.awt.GridLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.BorderFactory;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.plaf.ColorUIResource;

import service.Service;

@SuppressWarnings("serial")
public class AdjazenzmatrixPanel extends JPanel {

  private int knotenanzahl;
  private KnotenButton[][] buttons;

  public AdjazenzmatrixPanel(int knotenanzahl) {

    this.knotenanzahl = knotenanzahl;
    buttons = new KnotenButton[knotenanzahl][knotenanzahl];

    init();

    setVisible(true);
  }


  public KnotenButton getButton(int spalte, int zeile) {
    return buttons[spalte][zeile];
  }






  private void init() {
	  
    setLayout(new BorderLayout());
    setBorder(BorderFactory.createLineBorder(Color.BLACK));
    JPanel buttonsPanel = new JPanel(new GridLayout(knotenanzahl+1, knotenanzahl+1));
    JLabel textLabel = new JLabel("Adjazenzmatrix", JLabel.CENTER);
    textLabel.setFont(new Font("Serif", Font.BOLD, 20));
    textLabel.setBorder(BorderFactory.createRaisedBevelBorder());
    
    add(textLabel, BorderLayout.NORTH);
    add(buttonsPanel, BorderLayout.CENTER);
    
    for (int s = -1; s < knotenanzahl; s++) {
      for (int z =-1; z < knotenanzahl; z++) {
  
       if(z==-1 && s==-1)
         buttonsPanel.add(new JLabel(""));
       else
        if(z == -1)
          buttonsPanel.add(new JLabel((s+1)+"", JLabel.CENTER));
        else
       if(s == -1)
         buttonsPanel.add(new JLabel((z+1)+"", JLabel.CENTER));
       else
       {
        buttons[s][z] = new KnotenButton(s, z);
        buttons[s][z].setBorder(BorderFactory.createLineBorder(Color.BLACK));
        buttons[s][z].setAction(new AdjazenzAction(buttons[s][z]));
        buttons[s][z].setText("0");

        
       

        buttonsPanel.add(buttons[s][z]);

        if (buttons[s][z].getZeile() == buttons[s][z].getSpalte())
          buttons[s][z].setEnabled(false);

      }
      }
    }

  }

}
